package com.sq.partner.manager.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.FeeRateDao;
import com.sq.partner.manager.dao.ICoreDeptDao;
import com.sq.partner.manager.dao.IGatewayChannelInfoConfigDao;
import com.sq.partner.manager.mybatis.model.CoreDept;
import com.sq.partner.manager.mybatis.model.GatewayAuwConfig;
import com.sq.partner.manager.mybatis.model.GatewayAuwProd;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.RiskAlgMap;
import com.sq.partner.manager.mybatis.model.TradFeeRateAmnt;
import com.sq.partner.manager.mybatis.model.TradFeeRateDay;
import com.sq.partner.manager.mybatis.model.TradFeeRateFixed;
import com.sq.partner.manager.mybatis.model.TradFeeRateMonth;
import com.sq.partner.manager.mybatis.model.TradFeeRiskCommon;
import com.sq.partner.manager.service.FeeRateService;
import com.sq.partner.manager.service.TreddeFeeCovService;
import com.sq.partner.manager.util.Page;

@Service(value = "feeRateService")
public class FeeRateServiceImpl implements FeeRateService {

	@Resource
	private FeeRateDao feeRateDao;
	@Resource
	IGatewayChannelInfoConfigDao gatewayChannelInfoConfigDao;
	@Resource
	ICoreDeptDao deptDao;
	@Resource
	private TreddeFeeCovService tradeFeeCovService;
	@Override
	public <T> void batchInsertIntoFeeRate(String statementName, List<T> list) throws Exception {
		feeRateDao.batchInsertIntoFeeRate(statementName, list);
	}

	@Override
	public boolean batchInsertIntoRiskCommon(List<TradFeeRiskCommon> list) {
		return feeRateDao.batchInsertIntoRiskCommon(list);
	}

	@Override
	public void insertIntoFeeRateDay(TradFeeRateDay tradFeeRateDay) throws Exception {
		feeRateDao.insertIntoFeeRateDay(tradFeeRateDay);
	}

	@Override
	public List<TradFeeRateDay> queryTradFeeRateDay(TradFeeRateDay tradFeeRateDay) throws Exception {
		return feeRateDao.queryTradFeeRateDay(tradFeeRateDay);
	}

	@Override
	public void deleteTradFeeRateDayByRiskCode(String riskCode) throws Exception {
		feeRateDao.deleteTradFeeRateDayByRiskCode(riskCode);
	}

	@Override
	public void insertIntoFeeRateMonth(TradFeeRateMonth tradFeeRateMonth) throws Exception {
		feeRateDao.insertIntoFeeRateMonth(tradFeeRateMonth);
	}

	@Override
	public List<TradFeeRateMonth> queryTradFeeRateMonth(TradFeeRateMonth tradFeeRateMonth) throws Exception {
		return feeRateDao.queryTradFeeRateMonth(tradFeeRateMonth);
	}

	@Override
	public void deleteTradFeeRateMonthByRiskCode(String riskCode) throws Exception {
		feeRateDao.deleteTradFeeRateMonthByRiskCode(riskCode);
	}

	@Override
	public List<TradFeeRateFixed> queryTradFeeRateFixedById(String riskCode) throws Exception {
		return feeRateDao.queryTradFeeRateFixedById(riskCode);
	}

	@Override
	public void delTradFeeRateFixedById(String riskCode) throws Exception {
		feeRateDao.delTradFeeRateFixedById(riskCode);
	}

	@Override
	public void insertTradFeeRateFixed(TradFeeRateFixed feeRateFixed) throws Exception {
		feeRateDao.insertTradFeeRateFixed(feeRateFixed);
	}

	@Override
	public Object getDataListForDayOrMonth(List<Map<String, Object>> arr) throws Exception {

		String tradPlanCode = (String) arr.get(0).get("value");
		String calDefId = (String) arr.get(1).get("value");

		if ("0000000001".equals(calDefId)) {// 险种天数算法
			TradFeeRateDay tradFeeRateDay = null;
			BigDecimal premRate = null;
			List<TradFeeRateDay> list = new ArrayList<TradFeeRateDay>();

			for (int i = 2; i < arr.size(); i++) {
				tradFeeRateDay = new TradFeeRateDay();
				tradFeeRateDay.setRiskCode(tradPlanCode);
				tradFeeRateDay.setDays(Long.parseLong(arr.get(i).get("name").toString()));
				premRate = new BigDecimal(arr.get(i).get("value").toString());
				tradFeeRateDay.setPremRate(premRate);
				list.add(tradFeeRateDay);
			}
			return list;
		} else if ("0000000003".equals(calDefId)) {// 险种月数算法
			TradFeeRateMonth tradFeeRateMonth = null;
			BigDecimal fee = null;
			List<TradFeeRateMonth> list = new ArrayList<TradFeeRateMonth>();

			for (int i = 2; i < arr.size(); i++) {
				tradFeeRateMonth = new TradFeeRateMonth();
				tradFeeRateMonth.setRiskCode(tradPlanCode);
				tradFeeRateMonth.setMonths(Long.parseLong(arr.get(i).get("name").toString()));
				fee = new BigDecimal(arr.get(i).get("value").toString());
				tradFeeRateMonth.setPremRate(fee);
				list.add(tradFeeRateMonth);
			}
			return list;
		}

		return null;
	}

	// 根据险种id查询保额计算保费法表
	@Override
	public List<TradFeeRateAmnt> queryTradFeeRateRateAmntById(String riskCode) throws Exception {
		return feeRateDao.queryTradFeeRateRateAmntById(riskCode);
	}

	// 查询合作伙伴
	@Override
	public Page<GatewayChannelInfo> queryPageForGatewayChannelInfo(Page<GatewayChannelInfo> page) throws Exception {
		return gatewayChannelInfoConfigDao.getListByDept(page);
	}

	// 查询出单机构
	@Override
	public Page<CoreDept> queryPageForCoreDept(Page<CoreDept> page, String partnerNo) throws Exception {
		return deptDao.queryCoreDeptPage(page, partnerNo);
	}

	// 根据险种代码删除保额计算保费法表记录
	@Override
	public void delTradFeeRateAmntById(String riskCode) throws Exception {
		feeRateDao.delTradFeeRateAmntById(riskCode);
	}

	// 添加保额计算保费表
	@Override
	public void insertTradFeeRateAmnt(TradFeeRateAmnt tradFeeRateAmnt) throws Exception {
		feeRateDao.insertTradFeeRateAmnt(tradFeeRateAmnt);
	}

	@Override
	public Page<GatewayAuwConfig> queryAuwConfigsByPage(int size, int pageIndex, String tradProdCode) throws Exception {
		Page<GatewayAuwConfig> page = new Page<GatewayAuwConfig>();
		Map<String, Object> param = new HashMap<String, Object>();
		String beginRow = String.valueOf((pageIndex - 1) * size);
		String endRow = String.valueOf(pageIndex * size);
		param.put("tradProdCode", tradProdCode);
		param.put("beginRow", beginRow);
		param.put("endRow", endRow);
		int total = feeRateDao.queryTotalSizeOfAuwConfigs();
		page.setTotal(total);
		page.setCurrent(pageIndex);
		page.setSize(size);
		List<GatewayAuwConfig> list = feeRateDao.queryAuwConfigsByPage(param);
		page.setData(list);
		return page;
	}

	@Override
	public List<GatewayAuwProd> queryAuwConfigsByProd(String tradProdCode) throws Exception {
		return feeRateDao.queryAuwConfigsByProd(tradProdCode);
	}

	@Override
	public void removeAllAuwConfigsFromProd(String tradProdCode) throws Exception {
		feeRateDao.removeAllAuwConfigsFromProd(tradProdCode);
	}

	@Override
	public List<TradFeeRiskCommon> queryTradFeeRiskCommonByMap(Map<String, Object> map) throws Exception {
		return feeRateDao.queryTradFeeRiskCommonByMap(map);
	}

	/**
	 * 根据险种RiskCode删除表中记录
	 * 
	 */
	@Override
	public void delTradFeeRiskCommonByRiskCode(String tradPlanCode) throws Exception {
		feeRateDao.delTradFeeRiskCommonByRiskCode(tradPlanCode);
	}
	
	/**
	 * 查询数据库MaxId
	 * 
	 */
	@Override
	public int queryMaxId() throws Exception {
		return feeRateDao.queryMaxId();
	}

	/**
	 * 添加险种通用算法(每天保费固定乘以天数)
	 * 
	 */
	@Override
	public void insertTradFeeRiskCommon(TradFeeRiskCommon tradFeeRiskCommon) throws Exception {
		feeRateDao.insertTradFeeRiskCommon(tradFeeRiskCommon);
	}
	
	@Override
	public List<TradFeeRiskCommon> getDataListForTradFeeRiskCommon(List<Map<String, Object>> arr) throws Exception {
		List<TradFeeRiskCommon> list = new ArrayList<TradFeeRiskCommon>();
		TradFeeRiskCommon riskCommon = null;
		BigDecimal fee = null;

		String tradPlanCode = (String) arr.get(0).get("value");
		String childCalDefId = (String) arr.get(2).get("value");

		if ("000000000605".equals(childCalDefId)) {
			for (int i = 3; i < arr.size(); i += 4) {
				riskCommon = new TradFeeRiskCommon();
				riskCommon.setRiskCode(tradPlanCode);
				riskCommon.setCond7(arr.get(i).get("value").toString());
				riskCommon.setCond1(Long.parseLong(arr.get(i + 1).get("value").toString()));
				riskCommon.setCond2(Long.parseLong(arr.get(i + 2).get("value").toString()));
				fee = new BigDecimal(arr.get(i + 3).get("value").toString());
				riskCommon.setPremRate(fee);
				list.add(riskCommon);
			}
		} else if ("000000000602".equals(childCalDefId) || "000000000603".equals(childCalDefId)) {
			for (int i = 3; i < arr.size(); i += 3) {
				riskCommon = new TradFeeRiskCommon();
				riskCommon.setRiskCode(tradPlanCode);
				riskCommon.setCond7(arr.get(i).get("value").toString());
				String cond1 = arr.get(i + 1).get("value").toString();
				if (!"".equals(cond1) && cond1 != null) {
					riskCommon.setCond1(Long.parseLong(cond1));
				}
				fee = new BigDecimal(arr.get(i + 2).get("value").toString());
				riskCommon.setPremRate(fee);
				list.add(riskCommon);
			}
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> queryTradFeeRiskCommonByRiskCode(String riskCode) throws Exception {
		return feeRateDao.queryTradFeeRiskCommonByRiskCode(riskCode);
	}

	@Override
	public int checkOpenCalDiv(String tradPlanCode) throws Exception {
		return feeRateDao.checkOpenCalDiv(tradPlanCode);
	}

	@Override
	public void updateOps(RiskAlgMap riskAlgMap) throws Exception {
		feeRateDao.updateOps(riskAlgMap);
	}

	@Override
	public List<Map<String, Object>> impNlifeAuw(Map<String, Object> map)
			throws Exception {
		return feeRateDao.impNlifeAuw(map);
	}

	@Override
	public <T> void addNlifeBatchFeeRate(List<T> list,Map<String, Object> map) throws Exception {
		/**删除所有算法*/
		tradeFeeCovService.deleteTradeCov(map);
		/**批量插入*/
		this.batchInsertIntoFeeRate("inserttradeFeeCovNewLifeBach1", list);
	}

	@Override
	public void deletThiscal(String id) throws Exception {
		feeRateDao.deleteThisCal(id);
	}

	@Override
	public void updateThisCal(String prodCode, String riskCode, String id,
			String covCode, String cond1, String cond2, String cond3,
			String cond7, String cond10,String cond13 ,double premRate,String unused,String rationId) throws Exception {
			feeRateDao.updateThisCal(prodCode,riskCode,id,covCode,cond1,cond2,cond3,cond7,cond10,cond13,premRate,unused, rationId);
		
	}

	@Override
	public void insertThisCal(Map<String, Object> map) throws Exception {
		feeRateDao.insertThisCal(map);
	}

	@Override
	public String isExistCal(Map<String, String> map) throws Exception {
		
		return feeRateDao.isExistCal(map);
	}

	@Override
	public List<Map<String, BigDecimal>> queryCovUse3(Map<String, String> map) throws Exception {
		//查询所有年区间算法
		return feeRateDao.queryCovUse3(map);
	}

	@Override
	public String writeBackShortRate(Map<String, String> map) throws Exception {
		return feeRateDao.writeBackShortRate(map);
	}

	@Override
	public int queryExistShortRateByMap(Map<String, String> map)  throws Exception{
		return feeRateDao.queryExistShortRateByMap(map);
	}

	@Override
	public void updateShortRate(Map<String, String> map)throws Exception {
		
		feeRateDao.updateShortRate(map);
		
	}
}
